<?php
class JobXenserver extends Job implements IJob
{
  protected $vmName     = null;
  protected $vmUuid     = null;
  protected $powerState = null;
  
  public function doBackup()
  {
    $h = "";
    if($this->host!=null)
    {
      $h = " -s " . $this->host;
    }
    $v = "";
    if($this->vmUuid!=null)
    {
      $v = " uuid=" . $this->vmUuid;
    }
    $u = "";
    if($this->user!=null)
    {
      $u = " -u " . $this->user;
    }
    $p = "";
    if($this->password!=null)
    {
      $p = " -pw " . $this->password;
    }
    
    if($this->vmName==null)
    {
      return;
    }
    
    if(!is_dir(dirname($this->getDestinationBackup())))
    {
      mkdir(dirname($this->getDestinationBackup()), 0660, true);
    }    
    
    if($this->powerState=="halted")
    {
      $this->logger->info("Powerstate of the virtual machine is 'halted'. Try to backup via export.");
      $cmd = $this->pathXe . "xe vm-export filename=" . $this->getDestinationBackup() . $v . $h . $u . $p;
      $this->execCommando($cmd);
    }
    else
    {
      $this->logger->info("Powerstate of the virtual machine is '" . $this->powerState . "'. Try to backup via snapshot.");
      
      $cmd = $this->pathXe . "xe vm-snapshot new-name-label=Backup" . $v . $h . $u . $p;
      $output = $this->execCommando($cmd);
      $snapshotUuid = trim($output[0]);
      
      $cmd = $this->pathXe . "xe template-param-set is-a-template=false uuid=" . $snapshotUuid . $h . $u . $p;
      $this->execCommando($cmd);
      
      $cmd = $this->pathXe . "xe vm-export uuid=" . $snapshotUuid . " filename=" . $this->getDestinationBackup() . $h .$u . $p;
      $this->execCommando($cmd);
      
      $cmd = $this->pathXe . "xe vm-uninstall force=true uuid=" . $snapshotUuid . $h . $u . $p;
      $this->execCommando($cmd);
    }
    
    $cmds   = array();
    $this->execCommandos($cmds);
  }
  
  public function getFileExtensionBackup()
  {
    return ".xva";
  }
  
  public function getFileExtensionCompression()
  {
    return "";
  }
    
  public function getFileIdentifier()
  {
    return preg_replace("/[^A-Za-z0-9\.]+/", "", $this->vmName ) . "-" . $this->vmUuid;
  }
  
  public function getVmDetails()
  {
    if($this->vmName==null)
    {
      $h = "";
      if($this->host!=null)
      {
        $h = " -s " . $this->host;
      }
      $u = "";
      if($this->user!=null)
      {
        $u = " -u " . $this->user;
      }
      $p = "";
      if($this->password!=null)
      {
        $p = " -pw " . $this->password;
      }
      
      $cmd = $this->pathXe . "xe vm-list uuid=" . $this->vmUuid . $h . $u . $p;
      $output = $this->execCommando($cmd);
      
      $vmName = substr($output[1], 23);
      if($vmName==false)
      {
        $this->logger->error("Unknown virtual machine uuid: '" . $this->vmUuid . "'");
      }
      else
      {
        $this->vmName = $vmName;
        $this->powerState = substr($output[2], 23);
        $this->logger->info("Virtual machine details Name: '" . $this->vmName . "' Powerstate: '" . $this->powerState . "'");
      }
    }
    
    return $this->vmName;
  }
  
  public function postLoadJob()
  {
    $this->getVmDetails();
  }
  
  public function setOptions($optionsElement)
  {
    foreach($optionsElement->children() as $optionElement)
    {
      switch($optionElement->getName())
      {
        case "VmUuid":
          $this->setVmUuid((string)$optionElement);
          break;
                        
        default:
          $this->logger->warn("Unknown option element '" . $optionElement->getName() . "'.");
          break;
      }
    }
  }
  
  public function setVmUuid($vmUuid)
  {
    $this->vmUuid = $vmUuid;
  }
}